package com.hdd.art.biz.painting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdd.art.biz.member.service.MemberService;
import com.hdd.art.biz.painting.dto.PaintingWorksInfoVo;
import com.hdd.art.biz.painting.entity.PaintingWorks;
import com.hdd.art.biz.painting.mapper.PaintingWorksMapper;
import com.hdd.art.biz.painting.service.PaintingPraisesService;
import com.hdd.art.biz.painting.service.PaintingWorksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.art.biz.painting.service.WorkVerifyService;
import com.hdd.art.biz.show.entity.ArtShow;
import com.hdd.art.biz.show.entity.ShowroomScreen;
import com.hdd.art.biz.show.service.ArtShowService;
import com.hdd.art.utils.IdUtils;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 作品表 服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-06-27
 */
@Service
public class PaintingWorksServiceImpl extends ServiceImpl<PaintingWorksMapper, PaintingWorks> implements PaintingWorksService {



    @Autowired
    private WorkVerifyService verifyService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ArtShowService artShowService;
    @Autowired
    private PaintingPraisesService praisesService;



    ReentrantLock lock = new ReentrantLock();

    @Override
    public PaintingWorks findPaintingWorks(String workId) {
        QueryWrapper<PaintingWorks> wrapper  = new QueryWrapper<>();
        wrapper.eq("id",workId);
        return baseMapper.selectOne(wrapper);
    }


    @Override
    public List<PaintingWorks> getList(String artId, String roomId,Integer status,String isAdmin, Integer pageNo, Integer pageSize) {

        Map<String,Object> map = new HashMap<>();
        map.put("artId",artId);
        if(!StringUtils.isEmpty(roomId)) {
            map.put("roomId", roomId);
        }

        if(!StringUtils.isEmpty(isAdmin)){
            map.put("isAdmin", isAdmin);
        }
        map.put("status",status);
        map.put("pageNo",1);
        map.put("pageSize",20);
        return baseMapper.worksManage(map);
    }

    @Override
    public Long getListTotals(String artId, String roomId,Integer status) {
        Map<String,Object> map = new HashMap<>();
        map.put("artId",artId);
        if(!StringUtils.isEmpty(roomId)) {
            map.put("roomId", roomId);
        }
        map.put("status",status);
        return baseMapper.worksManageTotals(map);
    }


    @Override
    public void delete(String artId, String workId) {
        QueryWrapper<PaintingWorks> wrapper  = new QueryWrapper<>();
        wrapper.eq("id",workId);
        wrapper.eq("art_id",artId);
        baseMapper.delete(wrapper);
        return;
    }

    @Override
    public List<PaintingWorks> getMyWorkList(String userId, Integer pageNo, Integer pageSize) {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper<>();
        Page<PaintingWorks> pages = new Page<>(pageNo,pageSize);
        wrapper.eq("user_id",userId);
        wrapper.eq("del_flag",0);
        wrapper.orderByDesc("create_time");
        IPage<PaintingWorks> mapIPage = baseMapper.selectPage(pages, wrapper);
        return mapIPage.getRecords();

    }

    @Override
    public List<PaintingWorksInfoVo> getMyVerifyList(String userId, Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("pageNo",1);
        map.put("pageSize",20);

        List<PaintingWorksInfoVo> list =  baseMapper.getMyVerifyList(map);
        for(PaintingWorksInfoVo vo:list){
            vo.setVerifyStauts(1);
        }
        return list;
    }

    @Override
    public PaintingWorks workInfo(String workId) {
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper<>();
        wrapper.eq("id",workId);
        return baseMapper.selectOne(wrapper);
    }


    @Override
    public List<PaintingWorksInfoVo> myWorksDetails(String workId, String userId, Integer pageNo, Integer pageSize) {

        //TODO 默认第一张
        QueryWrapper<PaintingWorks> wrapper = new QueryWrapper<>();
        Page<PaintingWorks> pages = new Page<>(pageNo,pageSize);
        wrapper.eq("user_id",userId);
        wrapper.eq("del_flag",0);
        wrapper.orderByDesc("create_time");
        IPage<PaintingWorks> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingWorksInfoVo> worksInfoVos = new ArrayList<>();
        PaintingWorksInfoVo vo = null;
        for(PaintingWorks works:mapIPage.getRecords() ){
            vo = new PaintingWorksInfoVo();
            vo.setPainting(works.getPainting());
            vo.setMediaUrl(works.getMediaUrl());
            vo.setId(works.getId());
            vo.setUserId(works.getUserId());
            vo.setShare(100L);
            vo.setPraises(1001L);
            vo.setShows(works.getVisitCount());
            vo.setPraisess(praisesService.isPraises(works.getId(),userId));
            vo.setArtId(works.getArtId());
            vo.setRoomId(works.getRoomId());
            vo.setPaintingName(works.getPaintingName());
            vo.setPaintingDescription(works.getPaintingDescription());
           // vo.setStatus(works.getStatus());
            worksInfoVos.add(vo);
        }
        return worksInfoVos;

    }

    @Override
    public List<PaintingWorksInfoVo> artWorksDetails(String artId, String roomId,String userId,Integer status, Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("artId",artId);
        if(!StringUtils.isEmpty(roomId)) {
            map.put("roomId", roomId);
        }
        map.put("status",status);
        map.put("pageNo",1);
        map.put("pageSize",20);
        List<PaintingWorks>  list = baseMapper.worksManage(map);
        List<PaintingWorksInfoVo> worksInfoVos = new ArrayList<>();
        PaintingWorksInfoVo vo = null;
        for(PaintingWorks works:list){
            vo = new PaintingWorksInfoVo();
            vo.setPainting(works.getPainting());
            vo.setMediaUrl(works.getMediaUrl());
            vo.setRoomId(works.getRoomId());
            vo.setId(works.getId());
            vo.setUserId(works.getUserId());
            vo.setShare(100L);
            vo.setPraises(1001L);
            vo.setShows(works.getVisitCount());
            vo.setPraisess(praisesService.isPraises(works.getId(),userId));
            vo.setVid(works.getVid());
            vo.setArtId(works.getArtId());
            vo.setPaintingName(works.getPaintingName());
            vo.setPaintingDescription(works.getPaintingDescription());
            vo.setStatus(works.getStatus());
            worksInfoVos.add(vo);
        }
        return worksInfoVos;
    }

    @Override
    @Transactional
    public void publish(PaintingWorks works) {
        works.setId(IdUtils.getShartUUID());
        ArtShow artShow = artShowService.findArtShowByUserId(works.getUserId());
        if(artShow!=null){
            //TODO 直接加入作品库
            verifyService.insert(works.getId(),artShow.getArtId(),1);
        }
        baseMapper.insert(works);
    }


    @Override
    public void batchDelete(String artId,String ids) {
        String id[] = ids.split(",");

        for(int i=0;i<id.length;i++){

            verifyService.delete(artId,id[i]);
        }

    }

    @Override
    public void addShows(String workId) {
        lock = new ReentrantLock();
        lock.lock();
        try {
            baseMapper.addShows(workId);
        }catch (Exception e){
            e.printStackTrace();
            lock.unlock();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public void updateRoom(String workId, Integer roomId) {

        verifyService.updateRoom(workId,roomId);
    }

}
